package com.cx.controller.lq;

import com.cx.constant.ConstantData;
import com.cx.pojo.FriendInfo;
import com.cx.pojo.ResultData;
import com.cx.pojo.User;
import com.cx.service.lq.FriendService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("friendlist")
public class FriendController {
    @Autowired
    FriendService friendService;

    @RequestMapping("showallfriends")
    @ResponseBody
    public List<FriendInfo> selectAll(HttpSession session){
       User user = (User) session.getAttribute(ConstantData.SESSION_KEY);
       int userId = user.getUserId();
        System.out.println("userid"+userId);
        List<FriendInfo> friendInfos = friendService.selectAllFriend(userId);
        System.out.println(friendInfos.size());
        return friendInfos ;
    }

    @RequestMapping("addfriend")
    @ResponseBody
    public String addfriendByphone(HttpSession session,String userPhone){
        User user = (User) session.getAttribute(ConstantData.SESSION_KEY);
        int userId = user.getUserId();
        System.out.println(userId+userPhone);
        int affect = friendService.addfriendByphone(userId,userPhone);
        if(affect>0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping("deletefriend")
    @ResponseBody
    public String deleteByfriend( int friendUserId){
        int affect = friendService.deleteByfriend(friendUserId);
        if(affect>0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping("givegame")
    @ResponseBody
    public String givegame(int friendUserId,int gameId){
        int affect = friendService.givegame(friendUserId,gameId);
        if(affect>0)
            return "success";
        else
            return "fail";
    }
}
